import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Users from '@/components/user/Users'
import Welcome from '@/components/Welcome'
import Roles from '@/components/power/Roles'
import Rights from '@/components/power/Rights'
import Categories from '@/components/goods/Categories'
import Params from '@/components/goods/Params'
import List from '@/components/goods/List'
import Add from '@/components/goods/Add'
import Orders from '@/components/orders/Orders'
import Reports from '@/components/report/Reports'
import { storageGet } from '@/storage'
import config from '@/config'

Vue.use(VueRouter) //   这一步注册，就是调用vuerouter里面的install方法想

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      redirect: '/welcome',
      component: Home,
      children: [
        { path: '', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/Params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const tokenStr = storageGet(config.SignName)
  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
